"use strict";
import lscache from "lscache";
import hash from "object-hash";
import $ from "jquery";

import { pingAnChain } from "./numberformat.js";
import { UserActions } from "../action/useraction.js";


// var APIDOMAIN = 'http://222.247.45.147:8080';
// https://stock.pingan.com.cn/um/v1/a
const APIDOMAIN = 'https://stock.pingan.com.cn/exchange'
// const APIDOMAIN = 'http://stock.stg.pingan.com:443/exchange'
// var APIDOMAIN = 'http://a.com:8000/exchange'

var signToken = null;
var signTokenRequested = false;

var setToken = function(data){
    signToken = data.token;
}

var getToken = function() {
    if(signToken == null){
        console.warn("token is null");
    }
    return signToken;
}

var ajaxCall = function(url, params, callback){
    // console.log(url, params);
	// url = APIDOMAIN + url + "?_method=post&_json=";
    url = APIDOMAIN + url;
    // params['sign'] = 'J18AI1F4.F53A14CHDA0G8C7ND06IA82D820.EEE189D.CA385C1.7359A3EG644N471AE78.30AIAFA5705083BA6F2B1C2A6166049A01E9B929555790EE6E29C7F2';    
    params['sign'] = getToken();
    params = JSON.stringify(params);
    params = {_json: params, _method: 'GET', };    
	$.ajax({
		url:url,
        data:params,
        dataType:'jsonp',
        type: 'POST',
        // jsonp: "cb",
        beforeSend: function(xhr, settings) { xhr.setRequestHeader('contentType','application/json; charset=utf-8'); },
        error: function(XMLHttpRequest, textStatus, errorThrown){
        	// 错误处理
        },
        success: function(response){

        	if(response.err == 0){
        		var data = response.data;
        		// 计算可能的同比、环比
        		var data = pingAnChain(data);
        		if(callback){
	        		callback(data)
	        	}
        	}
        	else if(response.err == 4015){
        		// token 过期或无效, 重新登录
                console.log(signToken, '过期');
                UserActions.logout();
        		// throw 403;	
                // 自动重新登录
                // ?_method=get&_json={%22user%22:%22UMRIGHT%22,%22pwd%22:%22aaaaa888%22}&cb=ddddd
                // if(signTokenRequested == true){
                //     // 已经有请求
                //     return
                // }
                // signTokenRequested = true;
                // var params = {user:'UMRIGHT', pwd:'aaaaa888'};
                // params = JSON.stringify(params);
                // params = {_json: params, _method: 'GET'};
                // $.ajax({
                //     url: "http://stock.stg.pingan.com:443/um/v1/auth/login",
                //     data: params,
                //     dataType: 'jsonp',
                //     jsonp: "cb",
                //     type: 'GET',
                //     beforeSend: function(xhr, settings) { xhr.setRequestHeader('contentType','application/json; charset=utf-8'); },
                //     error: function(XMLHttpRequest, textStatus, errorThrown){},
                //     success: function(response){
                //         setTimeout(function(){signTokenRequested = false}, 60*1000)
                //     }
                // })
        	}
			else{
				// 错误处理, 开启
				console.warn("Api call fail:", url, response.Msg);
			}
		}
	});
};

export { ajaxCall, APIDOMAIN, setToken, getToken };